package com.dalin.platform.loa.service.imple;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dalin.common.utils.StringUtils;
import com.dalin.framework.service.imple.FrameServiceImple;
import com.dalin.platform.loa.condition.LoaDicTypeCondition;
import com.dalin.platform.loa.model.LoaDicItemEntity;
import com.dalin.platform.loa.model.LoaDicTypeEntity;
import com.dalin.platform.loa.service.DicService;
import com.dalin.platform.loa.vo.WsCurrentUserInfo;
import com.dalin.platform.loa.vo.WsDicItemVo;
/*****************************************************************************
 * @content: 字典表访问通用接口实现类                                                                                                   *
 * @author : 那建林                                                                                                                                 *
 * @crtDate: 2010-05-25                                                     *
 ****************************************************************************/
public class DicServiceImple extends FrameServiceImple implements DicService {

	public Map<String, List<WsDicItemVo>> queryAllProjectItems(Long projectId)
			throws Exception {
		Map<String, List<WsDicItemVo>> rtnMap = new HashMap<String, List<WsDicItemVo>>();
		LoaDicTypeCondition typeCond = new LoaDicTypeCondition();
		typeCond.setProjectId(projectId);
		List<LoaDicTypeEntity> typeEntyList = queryListByCond(
				typeCond.prepareParams());
		List<LoaDicItemEntity> itemEntyList = null;
		WsDicItemVo itemVo = null;
		for (LoaDicTypeEntity typeEnty : typeEntyList) {
			itemEntyList = typeEnty.getLoaDicItemEntitys();
			List<WsDicItemVo> dicVoList = new ArrayList<WsDicItemVo>();
			for (LoaDicItemEntity itemEnty : itemEntyList) {
				itemVo = new WsDicItemVo(itemEnty.getItemCode(), 
						itemEnty.getItemName());
				dicVoList.add(itemVo);
			}
			rtnMap.put(typeEnty.getTypeCode(), dicVoList);
		}
		return rtnMap;
	}
	
	
	public String findItemName(Long projectId, String typeCode, Integer itemCode)
			throws Exception {
		String rtnStr = "";
		LoaDicTypeCondition typeCond = new LoaDicTypeCondition();
		typeCond.setProjectId(projectId);
		typeCond.setTypeCode(typeCode);
		List<LoaDicTypeEntity> typeEntyList = queryListByCond(typeCond.prepareParams());
		if (StringUtils.isBlank(typeEntyList)) return rtnStr;
		List<LoaDicItemEntity> itemList = typeEntyList.get(0).getLoaDicItemEntitys();
		for (LoaDicItemEntity itemEnty : itemList) {
			if (itemCode.equals(itemEnty.getItemCode())) {
				rtnStr = itemEnty.getItemName();
				break;
			}
		}
		return rtnStr;
	}

	public List<WsDicItemVo> queryItems(Long projectId, String typeCode)
			throws Exception {
		List<WsDicItemVo> rtnList = new ArrayList<WsDicItemVo>();
		//WsCurrentUserInfo cui = LoaConst.getLoginedUserInfoByLum(lum);
		LoaDicTypeCondition typeCond = new LoaDicTypeCondition();
		//typeCond.setProjectId(cui.getProjectId());
		typeCond.setProjectId(projectId);
		typeCond.setTypeCode(typeCode);
		List<LoaDicTypeEntity> typeEntyList = super.queryListByCond(typeCond.prepareParams());
		if (StringUtils.isBlank(typeEntyList)) return rtnList;
		List<LoaDicItemEntity> itemEntyList = typeEntyList.get(0).getLoaDicItemEntitys();
		WsDicItemVo itemVo = null;
		for (LoaDicItemEntity itemEnty : itemEntyList) {
			itemVo = new WsDicItemVo(itemEnty.getItemCode(), itemEnty.getItemName());
			rtnList.add(itemVo);
		}
		return rtnList;
	}

	public List<WsDicItemVo> queryItemsByParent(WsCurrentUserInfo cui, String typeCode,
			String parentItemCode) throws Exception {
		List<WsDicItemVo> rtnList = new ArrayList<WsDicItemVo>();
		LoaDicTypeCondition typeCond = new LoaDicTypeCondition();
		typeCond.setProjectId(cui.getProjectId());
		typeCond.setTypeCode(typeCode);
		List<LoaDicTypeEntity> typeEntyList = queryListByCond(typeCond);
		if (StringUtils.isBlank(typeEntyList)) return rtnList;
		List<LoaDicItemEntity> itemEntyList = typeEntyList.get(0).getLoaDicItemEntitys();
		WsDicItemVo itemVo = null;
		for (LoaDicItemEntity itemEnty : itemEntyList) {
			if (parentItemCode == itemEnty.getParentItemCode()) {
				itemVo = new WsDicItemVo(itemEnty.getItemCode(), itemEnty.getItemName());
				rtnList.add(itemVo);
			}
		}
		return rtnList;
	}

	public List<WsDicItemVo> queryLoaItems(WsCurrentUserInfo cui, String typeCode)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public List<WsDicItemVo> queryLoaItemsByParent(WsCurrentUserInfo cui, String typeCode,
			Integer parentItemCode) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
